
import React from 'react';
import { Button, Row, Col, message } from 'antd';
import {
    WeaLocaleProvider,
    WeaDialog,
    WeaInput,
    WeaSearchGroup,
    WeaTools,
} from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
import './style/';

class Add extends React.Component {
    state = {
    }


    setSetting = (stateData) => {
        let setting = Object.assign({}, this.state.setting, stateData);
        this.setState({
            setting,
        });
    }

    onCancel = () => {
        this.setState({
            id: '',
            setting: {},
            parentId: '',
        });
        this.props.setStateData({
            addVisiable: false,
            setId: -1,
            isSaveAs: false,
        });
    }

    onRequired = () => {
        const { setting } = this.state;
        const { title } = setting;
        if (title.trim() != '') {
            return true;
        }
            return false;
    }

    onOk = () => {
        if (this.onRequired()) {
            const { javaInterface } = this.props;
            const { id, setting, parentId } = this.state;
            WeaTools.callApi(javaInterface.saveOrUpdateItem, 'POST', { json: JSON.stringify({ id, setting: JSON.stringify(setting), parentId }) }).then((result) => {
                if (result.status == true) {
                    this.props.onLoad({ handlerType: 'savaOrUpdateItem', id });
                    this.onCancel();
                    message.success(getLabel(83551, '保存成功！'), 3);
                } else {
                    message.error(getLabel(84544, '保存失败！'), 3);
                }
            });
        } else {
            message.warning(getLabel(15859, '必要信息不完整！'), 3);
        }
    }


    componentWillReceiveProps(nextProps) {
        const setId = nextProps.setId;
        const isSaveAs = nextProps.isSaveAs;
        const items = nextProps.data.itemsForSetting;
        let item;
        if (setId != -1) {
            for (let i in items) {
                if (items[i].id == setId) {
                    item = Object.assign({}, items[i]);
                    continue;
                }
            }
            if (isSaveAs) {
                item.id = -1;
            }
            this.setState({ ...item });
        } else {
            this.setState({
                id: setId,
                setting: {
                    title: '',
                },
                parentId: nextProps.data.id,
            });
        }
    }


    getButtons() {
        let buttons = [];
        buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@rij7m1`} type="primary" onClick={this.onOk}>{getLabel(826, '确定')}</Button>);
        buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@62i4kh`} type="ghost" onClick={this.onCancel}>{getLabel(31129, '取消')}</Button>);
        return buttons;
    }

    render() {
        const { uuid, visiable, AddExtend, AddExtendByEcode, AddExtendGroup, AddExtendGroupByEcode, data, setId } = this.props;
        const { setting = {} } = this.state;
        const { title_base64 } = setting;

        let _AddExtend = null;
        if (AddExtendByEcode && window.rewritePortalCusEleSetting && typeof rewritePortalCusEleSetting == 'function') {
            _AddExtend = window.rewritePortalCusEleSetting({ ...this.props, data, setting, setSetting: this.setSetting, setId }, { id: uuid, componentName: 'AddExtend' });
        }
        let _AddExtendGroup = null;
        if (AddExtendGroupByEcode && window.rewritePortalCusEleSetting && typeof rewritePortalCusEleSetting == 'function') {
            _AddExtendGroup = window.rewritePortalCusEleSetting({ ...this.props, data, setting, setSetting: this.setSetting, setId }, { id: uuid, componentName: 'AddExtendGroup' });
        }
        return (
            <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@rr18lh`}
              className="portal-esetting-dialog"
              visible={visiable}
              title={getLabel(19480, '内容设置')}
              icon="icon-coms-portal"
              iconBgcolor="#1a57a0"
              style={{ width: 660, height: 507 }}
              hasScroll={true}
              buttons={this.getButtons()}
              onCancel={this.onCancel}
            >
                <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@otof3u`} title={getLabel(81711, '基本信息')} needTigger={true} showGroup={true}>

                    <div className="set-items">
                        <div className="set-item">
                            <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@2pyxz2`} type="flex" align="middle">
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@gtb5pd`} span={6} >
                                    <div>{getLabel(508544, '标题')} :</div>
                                </Col>
                                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@vml3lk`} span={18}>
                                    <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@svir9g`} value={title_base64} viewAttr={3} inputType={'multilang'} isBase64={true} onChange={value => this.setSetting({ title: value, title_base64: value })} />
                                </Col>
                            </Row>
                        </div>
                        {
                            visiable == true && AddExtend ? (
                                <AddExtend ecId={`${this && this.props && this.props.ecId || ''}_AddExtend@j7x362`} ref="addExtend" {...this.props} data={data} setting={setting} setSetting={this.setSetting} setId={setId} />
                            ) : null

                        }
                        {
                            visiable == true && AddExtendByEcode ? (
                                _AddExtend
                            ) : null
                        }

                    </div>
                </WeaSearchGroup>
                {
                    visiable == true && AddExtendGroup ? (
                        <AddExtendGroup ecId={`${this && this.props && this.props.ecId || ''}_AddExtendGroup@ys9am8`} ref="addExtend" {...this.props} data={data} setting={setting} setSetting={this.setSetting} setId={setId} />
                    ) : null
                }
                {
                    visiable == true && AddExtendGroupByEcode ? (
                        _AddExtendGroup
                    ) : null
                }
            </WeaDialog>
        );
    }
}
export default Add;
